package person.drf.shop.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import person.drf.shop.product.entity.AttrEntity;
import person.drf.shop.product.entity.ProductAttrValueEntity;
import person.drf.shop.product.service.AttrService;
import person.drf.common.utils.PageUtils;
import person.drf.common.utils.R;
import person.drf.shop.product.service.ProductAttrValueService;
import person.drf.shop.product.vo.AttrGroupRelationVo;
import person.drf.shop.product.vo.AttrRespVO;
import person.drf.shop.product.vo.AttrVo;


/**
 * 
 *
 * @author dengrunfa
 * @email 1270733328@qq.com
 * @date 2020-04-07 15:45:24
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    @Autowired
    ProductAttrValueService productAttrValueService;

    //spu规格查询
    @GetMapping("/base/listforspu/{supId}")
    public R baseAttrList(@PathVariable("supId")Long supId){

        List<ProductAttrValueEntity> entities = productAttrValueService.baseAttrListforSpu(supId);

        return R.ok().put("data", entities);
    }

    //spu规格查询
    @PostMapping("/update/{supId}")
    public R updateSpuAttr(@PathVariable("supId")Long supId,
                           @RequestBody List<ProductAttrValueEntity> entities){

        productAttrValueService.updateSpuAttr(supId,entities);

        return R.ok();
    }


    //product/attr/sale/list?0
    //product/attr/base/list?0
    @GetMapping("/{attrType}/list/{catelogId}")
    public R baselist(@RequestParam Map<String, Object> params,
                      @PathVariable("catelogId") Long catelogId,
                       @PathVariable("attrType")String type){

        PageUtils page = attrService.queryBaseAttrPage(params,catelogId,type);

        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId){
        AttrRespVO attr = attrService.getAttrInfo(attrId);

        return R.ok().put("attr", attr);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attr:save")
    public R save(@RequestBody AttrVo attr){
        System.out.println(attr);
		attrService.saveAttr(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attr:update")
    public R update(@RequestBody AttrRespVO attr){
		attrService.updateAttr(attr);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }


}
